//
//  RHBrowsePictureView.m
//  RuiHeAppApp
//
//  Created by bill on 2023/8/19.
//

#import "RHBrowsePictureView.h"

@interface RHBrowsePictureView()<UIScrollViewDelegate>

@property (nonatomic ,strong) UIImageView *picture;

@end

@implementation RHBrowsePictureView

-(void)browsePictureFromRect:(CGRect)frame  imageView:(UIImage*)image
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.frame = CGRectMake(0, 0, kUI_WINDOW_W, kUI_WINDOW_H);
    UIView *grayView = [[UIView alloc]init];
    grayView.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.4];
    [self addSubview:grayView];
    [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsZero);
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [tap addTarget:self action:@selector(dismiss)];
    [grayView addGestureRecognizer:tap];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]init];
    [tap2 addTarget:self action:@selector(dismiss)];
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.minimumZoomScale = 1;
    scrollView.maximumZoomScale=5.0;
    [scrollView addGestureRecognizer:tap2];
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.delegate = self;
    [self addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_offset(0);
            make.centerY.mas_offset(0);
            make.height.equalTo(scrollView.mas_width);
    }];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    imageView.backgroundColor = [UIColor clearColor];
    imageView.frame = frame;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:imageView];
    self.picture = imageView;
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame = scrollView.frame;
    } completion:^(BOOL finished) {
        [scrollView addSubview:imageView];
        imageView.frame = scrollView.bounds;
    }];
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.picture;
}
-(void)dismiss
{
    [self removeFromSuperview];
}
@end
